﻿using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(TileMap))]
public class TileMapEditor : Editor {
	
	TileMap script;
	Vector3 oldTilePos = new Vector3();

	bool showSelectTileSection;
	bool isDragged = false;

	void OnEnable()
	{
		script = (TileMap)target;
		
#if UNITY_EDITOR
		if (!Application.isPlaying)
		{
			Tools.current = Tool.View;
		}
#endif
	}

	public override void OnInspectorGUI()
	{
		EditorGUILayout.LabelField("Width : ", script.Width.ToString());
		EditorGUILayout.LabelField("Height : ", script.Height.ToString());
		EditorGUILayout.LabelField("Number of Tiles : ", script.NumberOfTiles.ToString());

		EditorGUILayout.Space();

		EditorGUILayout.BeginHorizontal();
			var tmp = GUI.enabled;
			GUI.enabled = script.showTransparent = EditorGUILayout.Toggle("Transparency", script.showTransparent );
			
			script.alpha = EditorGUILayout.Slider(script.alpha, 0, 1.0f);
			GUI.enabled = tmp;
		EditorGUILayout.EndHorizontal();

		if (GUI.changed)
			EditorUtility.SetDirty(target);
	}


	/*

	public override void OnInspectorGUI()
	{
		serializedObject.Update();
		EditorGUIUtility.LookLikeInspector();

		//Array of tiles template

		EditorGUILayout.BeginHorizontal();
			EditorGUILayout.PropertyField(serializedObject.FindProperty("depth"));
		EditorGUILayout.EndHorizontal();

		serializedObject.ApplyModifiedProperties();

		if (GUILayout.Button("Initialize Map"))
		{
		}
			
		//if (GUI.changed)
			//EditorUtility.SetDirty(target);
	}	

	void OnSceneGUI()
	{
		int controlID = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive);

		if (Event.current.type == EventType.Layout)
			HandleUtility.AddDefaultControl(controlID);

		var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
		var tilePos = new Vector3();
		var currentEvent = Event.current;


		if (script.tileTemplate != null)
		{
			script.tileTemplate.SetActive(false);
			RaycastHit hit;

			if (Physics.Raycast(ray, out hit))
			{
				//Not hitting base plane
				//Top of box
				if (hit.normal.y > 0)
				{
					tilePos = hit.collider.gameObject.transform.position + new Vector3(-0.5f, 1f, -0.5f);
				}
					//bot of box
				else if (hit.normal.y < 0)
				{
					tilePos = hit.collider.gameObject.transform.position + new Vector3(-0.5f, -1f, -0.5f);
				}

			}

			script.tileTemplate.SetActive(true);

			if (currentEvent.type == EventType.MouseDrag)
			{
				isDragged = true;
				var name = string.Format("({0},{1},{2})", tilePos.x, tilePos.y, tilePos.z);
				if (currentEvent.button == 0) //Left-click mouse
				{
					//Create Tile at tilePos
				}
			}

			if (currentEvent.type == EventType.MouseUp)
			{
				var name = string.Format("({0},{1},{2})", tilePos.x, tilePos.y, tilePos.z);

				if (!isDragged)
				{
					if (currentEvent.button == 0) //Left-click mouse
					{
						//Create Tile at tilePos
					}
				}
				isDragged = false;
			}
		}

		if (tilePos != oldTilePos)
		{
			script.gizmoPosition = tilePos;
			SceneView.RepaintAll();
			oldTilePos = tilePos;
		}

		//Update script only if GUIChanged
		if (GUI.changed)
			EditorUtility.SetDirty(target);	
	}*/

}
